home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
FromTheMag
/
JW FLV MEDIA PLAYER 4.2
/
mediaplayer.exe
/
player.swf
/
scripts
/
com
/
jeroenwijering
/
models
/
VideoModel.as
< prev
next >
Wrap
Text File
|
2008-11-04
|
7KB
|
221 lines
package com.jeroenwijering.models
{
import com.jeroenwijering.events.ModelEvent;
import com.jeroenwijering.events.ModelStates;
import com.jeroenwijering.player.Model;
import com.jeroenwijering.utils.NetClient;
import flash.events.AsyncErrorEvent;
import flash.events.ErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.media.SoundTransform;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.ObjectEncoding;
import flash.utils.clearInterval;
import flash.utils.setInterval;
public class VideoModel implements ModelInterface
{
private var stream:NetStream;
private var timeinterval:Number;
private var connection:NetConnection;
private var model:Model;
private var loadinterval:Number;
private var transform:SoundTransform;
private var metadata:Boolean;
private var video:Video;
public function VideoModel(param1:Model)
{
super();
model = param1;
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
connection.objectEncoding = ObjectEncoding.AMF0;
connection.connect(null);
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
stream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,metaHandler);
stream.bufferTime = model.config["bufferlength"];
stream.client = new NetClient(this);
video = new Video(320,240);
video.attachNetStream(stream);
transform = new SoundTransform();
stream.soundTransform = transform;
quality(model.config["quality"]);
if(model.config["mute"] == true)
{
volume(0);
}
else
{
volume(model.config["volume"]);
}
}
public function stop() : void
{
if(stream.bytesLoaded != stream.bytesTotal)
{
stream.close();
}
stream.pause();
metadata = false;
clearInterval(loadinterval);
clearInterval(timeinterval);
}
private function loadHandler() : void
{
var _loc1_:* = undefined;
var _loc2_:* = undefined;
_loc1_ = stream.bytesLoaded;
_loc2_ = stream.bytesTotal;
model.sendEvent(ModelEvent.LOADED,{
"loaded":_loc1_,
"total":_loc2_
});
if(_loc1_ == _loc2_ && _loc1_ > 0)
{
clearInterval(loadinterval);
}
}
private function metaHandler(param1:ErrorEvent) : void
{
model.sendEvent(ModelEvent.META,{"error":param1.text});
}
private function timeHandler() : void
{
var _loc1_:* = undefined;
var _loc2_:* = undefined;
var _loc3_:* = undefined;
_loc1_ = Math.round(stream.bufferLength / stream.bufferTime * 100);
_loc2_ = Math.round(stream.time * 10) / 10;
_loc3_ = model.playlist[model.config["item"]]["duration"];
if(_loc1_ < 100 && _loc2_ < Math.abs(_loc3_ - stream.bufferTime * 2))
{
model.sendEvent(ModelEvent.BUFFER,{"percentage":_loc1_});
if(model.config["state"] != ModelStates.BUFFERING && _loc1_ < 10)
{
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
}
}
else if(model.config["state"] == ModelStates.BUFFERING)
{
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
}
if(_loc3_ > 0)
{
model.sendEvent(ModelEvent.TIME,{
"position":_loc2_,
"duration":_loc3_
});
}
}
private function statusHandler(param1:NetStatusEvent) : void
{
if(param1.info.code == "NetStream.Play.Stop" && stream.bytesLoaded == stream.bytesTotal)
{
clearInterval(timeinterval);
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.COMPLETED});
}
else if(param1.info.code == "NetStream.Play.StreamNotFound")
{
stop();
model.sendEvent(ModelEvent.ERROR,{"message":"Video not found: " + model.playlist[model.config["item"]]["file"]});
}
model.sendEvent(ModelEvent.META,{"info":param1.info.code});
}
public function volume(param1:Number) : void
{
transform.volume = param1 / 100;
stream.soundTransform = transform;
}
private function errorHandler(param1:ErrorEvent) : void
{
model.sendEvent(ModelEvent.ERROR,{"message":param1.text});
}
public function load() : void
{
model.mediaHandler(video);
stream.play(model.playlist[model.config["item"]]["file"]);
loadinterval = setInterval(loadHandler,100);
timeinterval = setInterval(timeHandler,100);
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
}
public function onData(param1:Object) : void
{
if(param1.type == "metadata" && !metadata)
{
metadata = true;
if(param1.width)
{
video.width = param1.width;
video.height = param1.height;
}
if(model.playlist[model.config["item"]]["start"] > 0)
{
seek(model.playlist[model.config["item"]]["start"]);
}
}
model.sendEvent(ModelEvent.META,param1);
}
public function play() : void
{
stream.resume();
timeinterval = setInterval(timeHandler,100);
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
}
public function seek(param1:Number) : void
{
clearInterval(timeinterval);
stream.seek(param1);
play();
}
public function pause() : void
{
clearInterval(timeinterval);
stream.pause();
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PAUSED});
}
public function quality(param1:Boolean) : void
{
if(param1 == true)
{
video.smoothing = true;
video.deblocking = 3;
}
else
{
video.smoothing = false;
video.deblocking = 1;
}
}
}
}